﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Srvtools;

public partial class WorkTime_ImportWorkDate : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;
    static public ArrayList hif = new ArrayList(); // 保存文件列表
    public int filesUploaded = 0; // 上传文件的数量

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int year = DateTime.Now.Year;
            int Month = DateTime.Now.Month;

            DlYear.SelectedValue = year.ToString();
            DLMonth.SelectedValue = Month.ToString();


            InitializeComponent();

            Master.DataSource = WMaster;

            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/考勤数据维护.htm";
            mpg.PageName = "考勤数据维护";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }

        PageErrLabel.Visible = false;
        PageErrLabel.Text = "";


    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTime_ImportWorkDate));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SWorkTime.WorkTime_WorkOn";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }


    protected object GetOnDuty()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);
    }

    protected object GetOffDuty()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 0, 0);
    }
    /// <summary>
    /// 将要上传的文件添加到listbox中
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void AddFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true)
        {
            string ss = FindFile.Value.Trim();
            if (ss.Length > 0)
            {
                hif.Add(FindFile);
                FileList.Items.Add(FindFile.PostedFile.FileName);
            }
        }
        else
        { }

    }

    /// <summary>
    /// 从listbox中删除指定的文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void RemvFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)
        {
            PageErrLabel.Text = "错误 - 必须指定要删除的文件.";
            PageErrLabel.Visible = true;
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            hif.RemoveAt(FileList.SelectedIndex);
            FileList.Items.Remove(FileList.SelectedItem.Text);
            //TipInfo.Text = "";
        }

    }

    /// <summary>
    /// 循环上传listbox中的文件到指定的文件夹下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Upload_ServerClick(object sender, System.EventArgs e)
    {

        string status = "";  // 上传成功后显示的文件列表   
        // get the applications path
        //string tempFile = Request.PhysicalApplicationPath;
        string FileFoldName = ConfigurationManager.AppSettings["WorkTimeWorkOnFilesFold"];


        if ((FileList.Items.Count == 0) && (filesUploaded == 0))
        {
            PageErrLabel.Text = "错误 - 必须指定要上传的文件.";
            PageErrLabel.Visible = true;
            return;
        }
        else
        {
            foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in hif)
            {
                try
                {
                    string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                    HIF.PostedFile.SaveAs(string.Format("{0}\\{1}", FileFoldName, fn));
                    filesUploaded++;
                    status += fn + "<br>";

                }
                catch (Exception err)
                {
                    PageErrLabel.Text = "上传错误 " + FileFoldName + "<br>" + err.ToString();
                    PageErrLabel.Visible = true;
                }
            }

            if (filesUploaded == hif.Count)
            {
                PageErrLabel.Text = "共上传了 " + filesUploaded + " 个文件。 <br>" + status;
                PageErrLabel.Visible = true;
            }
            hif.Clear();
            FileList.Items.Clear();
            DoImportData();
        }

    }

    protected void DoImportData()
    {
        //string tempFile = Request.PhysicalApplicationPath;
        string FileFoldName = ConfigurationManager.AppSettings["WorkTimeWorkOnFilesFold"];
        //string ss =  FileFoldName; //

        object[] objParam = new object[3];
        objParam[0] = FileFoldName; // @"C:\Program Files\InfoLight\EEP2006\EEPWebClient\uploads";
        objParam[1] = DlYear.SelectedValue;
        objParam[2] = DLMonth.SelectedValue;

        object[] myRet = CliUtils.CallMethod("SWorkTime", "BatchImportWorkOnDate", objParam);
        if (myRet != null && (int)myRet[0] == 0)
        {
            if (myRet.Length > 3)
            {
                int FilesCount = (int)myRet[1];
                int HaveDoReconCount = (int)myRet[2];
                string NoEmpsCardList = (string)myRet[3];
                string FilesNameStr = (string)myRet[4];
                PageErrLabel.Text = string.Format("<font size='3'>导入提示:</font></BR> 1.完成了{0}个文件导入({1});</BR> 2.有{2}考勤记录,</BR> 3.无效考勤卡号有:{3}", FilesCount, FilesNameStr, HaveDoReconCount, NoEmpsCardList);
                PageErrLabel.Visible = true;
            }
            else
            {
                PageErrLabel.Text = "无法正确导入考勤数据!";
                PageErrLabel.Visible = true;
            }
        }
        // string message = @"完成了文件数据导入";
        //this.Page.Response.Write("<script>alert(\"" + PageErrLabel.Text + "\");</script>");
    }

    protected void Refresh_Click(object sender, EventArgs e)
    {
        string Sql = "";
        Sql = string.Format(" (YEAR(WorkTime_WorkOn.OnDuty) = {0})  and (MONTH(WorkTime_WorkOn.OnDuty) = {1}) ", DlYear.SelectedValue, DLMonth.SelectedValue);
        if (SelUser.EmpName.Length > 0) Sql = Sql + " and (Employees.EmpName like '%" + SelUser.EmpName + "%' ) ";
        if ((DepartmentDList1.SelectDepmanetId.Length > 0) && (DepartmentDList1.SelectDepmanetId != "0")) Sql = Sql + " and (Employees.DepartmentId Like '" + DepartmentDList1.SelectDepmanetId + "%' ) ";

        Master.SetWhere(Sql);
        wgvMaster.DataBind();

    }
 
}
